package com.chy.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.chy.pojo.Catalog;
import com.chy.service.CatalogService;
import com.common.ServiceMessage;

@RestController
@RequestMapping("/admin/catalog")
@CrossOrigin
public class CatalogController {
   
    
    @Autowired
    private CatalogService catalogService;
    
    @SuppressWarnings("rawtypes")
    @GetMapping("/{pid}")
    public ServiceMessage getChildrenByPid(@PathVariable long pid) {
        try {
            List<Catalog> list = catalogService.getByPid(pid);
            return ServiceMessage.ok(list);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return ServiceMessage.error();
        }
    }
    
    @GetMapping("/select")
    public Catalog getCatalogById(long id) {
            Catalog catalog = catalogService.getCatalogById(id);
            return catalog;
    }
    
    @PostMapping("/insert")
    public int addCatalog(@RequestBody Catalog catalog) {
            System.out.println(catalog);
            return catalogService.add(catalog);
    }
    
    @SuppressWarnings("rawtypes")
    @DeleteMapping("/{id}")
    public ServiceMessage delCatalog(Catalog catalog) {
        catalogService.delete(catalog);
        return ServiceMessage.ok();
    }

}
